package Reports;


import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import TableAdditions.main;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JComboBox;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;



public class SortByVehicle extends JPanel{
	//private final JButton createTableButton = new JButton("Başla");
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);
	public SortByVehicle() {
		    JPanel startingDatePanel = new JPanel();
		    
	        String[] columnNames = {"Araç Plaka", "Sigorta Bitişi", "Son Bakım Tarihi","Gelecek Bakım Tarihi"};

	        for(int i=0;i<columnNames.length;i++)
	        	model.addColumn(columnNames[i]);	//add columns
	        
	        table.setPreferredScrollableViewportSize(new Dimension(920, 600));
	        table.setFillsViewportHeight(true);
		    final JFrame startFrame = new JFrame();
		    add(startingDatePanel);
		    JPanel finishDatePanel = new JPanel();
		    
		    JLabel lblNewLabel = new JLabel("          ");
		    finishDatePanel.add(lblNewLabel);
		    final JFrame finishFrame = new JFrame();
		    add(finishDatePanel);
		    
		    
		
		setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
	 	
		
		
		
		
		
		
		for( int m = model.getRowCount() - 1; m >= 0; m-- ) {
	        model.removeRow(m);
	    }
	 	Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        ResultSet rs2 = null;
       
        String url = "jdbc:mysql://localhost:3306/mydb";
        String user = "root";
        String password = "qweasdzxc";
        Object[][] mydata=null;
        int i=0,counter=0;
        try {
            con = (Connection) DriverManager.getConnection(url, user, password);
            st = (Statement) con.createStatement(); 
          /*  rs=st.executeQuery("SELECT * from bill" +
            		" WHERE product_id IN ( SELECT product_id from product WHERE product_name='"+ product + "') AND bill_date BETWEEN '"+startingDate2+"' AND '"+finishDate2+"' Order By bill_date") ;*/
            rs=st.executeQuery("SELECT * from vehicle order by vehicle_next_inspection");
        if(rs.isBeforeFirst()==true){
            while(rs.next())
            	++counter;
            rs.first();
            mydata=new Object[counter+1][6];
            String insuranceEndDate,lastInspectionDate,nextInspectionDate;
            int amount,amountsum=0;
            float specialPrice,specialPriceSum=0,billCost,billCostSum=0;
            java.text.NumberFormat nf = new java.text.DecimalFormat("0.00");
            //String[] customerCodes = new String[counter];
            
            do
            {	
            		mydata[i][0]=rs.getString("vehicle_licence_plate");
            		insuranceEndDate = rs.getString("vehicle_insurance_enddate").replaceAll("(\\d+)-(\\d+)-(\\d+)", "$3-$2-$1");
            		mydata[i][1]=insuranceEndDate;
            		//customerCodes[i]=new String();
            		lastInspectionDate=rs.getString("vehicle_last_maintenance").replaceAll("(\\d+)-(\\d+)-(\\d+)", "$3-$2-$1");
            		mydata[i][2]=lastInspectionDate;
            		//customerCodes[i]=(String) mydata[i][2];
            		//rs2.next();
            		//mydata[i][2]=rs2.getString("customer_name");
            		nextInspectionDate=rs.getString("vehicle_last_maintenance").replaceAll("(\\d+)-(\\d+)-(\\d+)", "$3-$2-$1");
            		mydata[i][3]=nextInspectionDate;
            		++i;
        
            }while(rs.next());
            
      /*      for(int j=0;j<counter;j++){
            	System.out.println("customerCode = "+customerCodes[j]);
            }*/
            
           
            
            
           // mydata[i][0]="Toplam";
            //mydata[i][3]=amountsum;
            //mydata[i][4]=nf.format(specialPriceSum);
            //mydata[i][5]=nf.format(billCostSum);
            
            
            for(int i1=0;i1<mydata.length;i1++)
            	model.addRow(mydata[i1]);
           
        }else {
            int messageType = JOptionPane.PLAIN_MESSAGE;
            
            JOptionPane.showMessageDialog(null, "Böyle bir kayıt yok", "Uyarı!!", messageType);
            
        }
            con.close();
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(main.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);

        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (st != null) {
                    st.close();
                }
                if (con != null) {
                    con.close();
                }

            } catch (SQLException ex) {
                Logger lgr = Logger.getLogger(main.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
            }
        }
	    table.setRowHeight(30);
	   
	  	
 	    
	    DefaultTableCellRenderer rdt = new DefaultTableCellRenderer(){
	    	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean hasFocus, int row, int column) {
	    		 Component c = 
	    			      super.getTableCellRendererComponent(table, value,
	    			                                          isSelected, hasFocus,
	    			                                          row, column);
	    	if (value == null)
	    	return super.getTableCellRendererComponent(table,
	    	value, isSelected, hasFocus, row, column);
	    	if (row != table.getRowCount()-1 )
	    		c.setFont(new Font("Helvetica Bold", Font.PLAIN,16));
	    	else
	    		c.setFont(new Font("Helvetica Bold", Font.BOLD,16));
	    	
	    	return c;
	    	}
	    	};
		    DefaultTableCellRenderer rightRenderer = new DefaultTableCellRenderer(){
		    	public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean hasFocus, int row, int column) {
		    		 Component c = 
		    			      super.getTableCellRendererComponent(table, value,
		    			                                          isSelected, hasFocus,
		    			                                          row, column);
		    	if (value == null)
		    	return super.getTableCellRendererComponent(table,
		    	value, isSelected, hasFocus, row, column);
		    	if (row != table.getRowCount()-1 )
		    		c.setFont(new Font("Helvetica Bold", Font.PLAIN,16));
		    	else
		    		c.setFont(new Font("Helvetica Bold", Font.BOLD,16));
		    	
		    	
		    	return c;
		    	}
		    	};
	        TableColumn column = new TableColumn();
	        for (int j = 0; j < columnNames.length; j++) {
	            column = table.getColumnModel().getColumn(j);
	            column.setPreferredWidth(15);
	            if(j<3)
	            	column.setCellRenderer(rdt);
	            else{
	            	rightRenderer.setHorizontalAlignment(SwingConstants.RIGHT);
	            	column.setCellRenderer(rightRenderer);
	            	
	            }

	         
	          
	            
	        }
	    JScrollPane scrollPane = new JScrollPane(table);
	      //Add the scroll pane to this panel.
	    add(scrollPane);
	    JButton PrintTableBtn = new JButton("Yazdır");
	    PrintTableBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				  PageFormat documentPageFormat = new PageFormat();
				    documentPageFormat.setOrientation(PageFormat.PORTRAIT);
				    MessageFormat header = new MessageFormat("İki Tarih Arası Ürün Satış Tablosu");
				    try {
				    	//MessageFormat format = new MessageFormat("{0}");
				    	MessageFormat format = new MessageFormat("Sayfa {0,number,integer}");
				        table.print(JTable.PrintMode.FIT_WIDTH, header, format);
				    } catch (java.awt.print.PrinterException e1) {
				        System.err.format("Cannot print %s%n", e1.getMessage());
				    }
			
			}
		});
	    
	    add(PrintTableBtn);
	  //  
        setVisible(true);
    }
	   
	       
	
	/*public static void main(String[] args) {
		 SortByProduct myInterface=new SortByProduct();
		 myInterface.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		 myInterface.setSize(980, 750);
		 Toolkit kit = myInterface.getToolkit();
		 Dimension screenSize = kit.getScreenSize();
		 int screenWidth = screenSize.width;
		 int screenHeight = screenSize.height;
		 Dimension windowSize = myInterface.getSize();
		 int windowWidth = windowSize.width;
		 int windowHeight = windowSize.height;
		 int upperLeftX = (screenWidth - windowWidth)/2;
		 int upperLeftY = (screenHeight - windowHeight)/2;
		 myInterface.setLocation(upperLeftX, upperLeftY);
		 myInterface.setVisible(true);
		
	}*/


}
